﻿///http://blogs.msdn.com/b/tomholl/archive/2013/03/02/integrate-lync-into-your-intranet-sites-using-the-namectrl-plug-in.aspx

/// <reference path="typings/jquery/jquery.d.ts" />
/// <reference path="typings/jquery/jqueryui.d.ts" />

module CloudStorageLight {

    export class LyncUtil {
        static nameCtrl: any;

        static init(): void {
            if (LyncUtil.nameCtrl) return;
            try {
                if (ActiveXObject !== undefined) {
                    LyncUtil.nameCtrl = new ActiveXObject("Name.NameCtrl");

                } else {
                    LyncUtil.nameCtrl = LyncUtil.createNPApiOnWindowsPlugin("application/x-sharepoint-uc");
                }
                LyncUtil.attachLyncPresenceChangeEvent();
            }
            catch (ex) { }
        }

        static isSupportedNPApiBrowserOnWin(): boolean {
            return true; // SharePoint does this: IsSupportedChromeOnWin() || IsSupportedFirefoxOnWin()
        }

        static isNPAPIOnWinPluginInstalled(a: any): boolean {
            return Boolean(navigator.mimeTypes) && navigator.mimeTypes[a] && navigator.mimeTypes[a].enabledPlugin
        }

        static createNPApiOnWindowsPlugin(b: any): any {
            var c = null;
            if (LyncUtil.isSupportedNPApiBrowserOnWin())
                try {
                    c = document.getElementById(b);
                    if (!Boolean(c) && LyncUtil.isNPAPIOnWinPluginInstalled(b)) {
                        var a = document.createElement("object");
                        a.id = b;
                        a.type = b;
                        a.width = "0";
                        a.height = "0";
                        a.style.setProperty("visibility", "hidden", "");
                        document.body.appendChild(a);
                        c = document.getElementById(b)
                }
                }
                catch (d) {
                    c = null;
                }
            return c;
        }


        static addUserPanel(userName: string, displayName: string, cont: JQuery): void {

            cont.unbind()
                .bind("mouseover", function () { LyncUtil.showLyncPresencePopup(userName, this); })
                .bind("mouseout", function () { LyncUtil.hideLyncPresencePopup(); });
        }

        static getLyncPresenceString(status: any): string {

            switch (status) {
                case 0:
                    return 'available';
                case 1:
                    return 'offline';
                case 2:
                case 4:
                case 16:
                    return 'away';
                case 3:
                case 5:
                    return 'inacall';
                case 6:
                case 7:
                case 8:
                case 10:
                    return 'busy';
                case 9:
                case 15:
                    return 'donotdisturb';
                default:
                    return '';
            }
        }

        static attachLyncPresenceChangeEvent(): void {
            if (!LyncUtil.nameCtrl) {
                return;
            }
            LyncUtil.nameCtrl.OnStatusChange = LyncUtil.onLyncPresenceStatusChange;

        }


        static onLyncPresenceStatusChange(userName, status, id): void {
            var presenceClass = LyncUtil.getLyncPresenceString(status);

            var userElementId = LyncUtil.getId(userName);
            var userElement = $('[data-userid="' + userElementId + '"]');
            LyncUtil.removePresenceClasses(userElement);
            userElement.addClass(presenceClass);
        }

        static removePresenceClasses(jqueryObj): void {
            jqueryObj.removeClass('available');
            jqueryObj.removeClass('offline');
            jqueryObj.removeClass('away');
            jqueryObj.removeClass('busy');
            jqueryObj.removeClass('donotdisturb');
            jqueryObj.removeClass('inacall');
        }

        static getId(userName): string {
            return userName;
        }

        static showLyncPresencePopup(userName, target): void {
            if (!LyncUtil.nameCtrl) {
                return;
            }

            var eLeft = $(target).offset().left;
            var x = eLeft - $(window).scrollLeft();

            var eTop = $(target).offset().top;
            var y = eTop - $(window).scrollTop();

            try {
                LyncUtil.nameCtrl.ShowOOUI(userName, 0, x, y);
            } catch (e) {
            }
        }

        static hideLyncPresencePopup(): void {
            if (!LyncUtil.nameCtrl) {
                return;
            }
            LyncUtil.nameCtrl.HideOOUI();
        }
    }
}